/**
 * 会员中心会议室订单
 */
$(document).ready(function(e) {	
	var $time= new Date();

	initialSpace();
$("#searchBeginTime").val(getDay());
$("#searchEndTime").val(getDay());
	//detail(1);
	search(1);
	//initialClientLevle();
})
function getDay(){
	var $time= new Date();
	$year = parseInt($time.getFullYear());
	$month = parseInt($time.getMonth())+1;
	$day = parseInt($time.getDate());
	if($month<10){
		$month ='0'+ $month;
	}
	if($day<10){
		$day ='0' + $day;
	}
	return $year +"-"+ $month +"-"+$day;	
}

function getDays($m,$n){
	if(!$n){
		$n='-';
	}

	var $time= new Date();
	$year = parseInt($time.getFullYear());
	$month = parseInt($time.getMonth())+1;
	$day = parseInt($time.getDate());
	if(!$m){
		return $year +"-"+ $month +"-"+$day;	
	}
	if($n=='-'){
		$month =$month-$m;	
	}else{
		$month =$month+$m;	
	}

	if($month<=0){
		$year =$year -1;
		$month = $month +12;
	}else if($month>12){
		$year =$year +1;
		$month =1;
	}
	if($month<10){
		$month ='0'+$month;
	}
	if($day<10){
		$day ='0'+$day;
	}
	return $year +"-"+ $month +"-"+$day;	
}
/*初始化选项*/
function searchReset(){
	$("#searchBeginTime").val(getDay());
	$("#searchEndTime").val(getDay());
	$("#searchStatus").val(-1);
	$("#searchOrderStatus").val(-1);
	$("#searchUserName").val('');
	$("#searchApplyUserName").val('');
	$("#searchMeetingRoomName").val('');
}

function getSearchParameter(currentPage){
	
		var searchTitle= $("#searchTitle").val();	
		var btime = $("#searchBeginTime").val(),
			etime = $("#searchEndTime").val(),
			//areaid = parseInt($("#searchSpace").val()),
			
			payStatus	= parseInt($("#searchOrderStatus").val()),
			status	= parseInt($("#searchStatus").val()),
			uname	= $("#searchApplyUserName").val(),
			rname	= $("#searchMeetingRoomName").val();
		//if(status==0) status=-1;
		if(!currentPage) currentPage=1;
		if(!uname) uname="";

		if(!rname) rname="";

		requestJson ={"endTime":etime,"startTime":btime,"status":status,"payStatus":payStatus,"pn":currentPage,"ps":10};
		return requestJson;
	
}

function detail(id){
	$("DIV[name='meetingRoomList']").hide();
	$("DIV[name='viewInfo']").show();	
	var memberToken = localStorage.getItem('memberToken');
	$.ajax({
        url: '/euc/_onlineops/getOneMyMeeting',
        data:{"order_id":id},
        beforeSend: function(request) {
            request.setRequestHeader("token",memberToken );
        },
        dataType: 'JSON',
        async: true,
        contentType:'application/x-www-form-urlencoded',
        type: 'POST',
        success: function (result, textStatus, response) {
        	//alert(JSON.stringify(result));
        	//var clientLevle = $("SELECT[name='clientLevle']");     
        	data=result.data;
        	$html='<tr><td>订单编号：</td><td>'+data.orderCode+'</td></tr>';
        	$html+='<tr><td>下单时间：</td><td>'+data.createTime+'</td></tr>';
           	$("#ViewInfo1").html($html);
           		$members='';
           		console.log(data.members);
           	$.each(data.members,function(i,v){
           		console.log(v);
           		$members+= v.name;
           	});
           	if(data.status==1){
           		$("#btnViewsave").show();
           		$("#btnViewsave").attr('data-id',data.orderId);
           	}else{
           		$("#btnViewsave").hide();
           	}
           	$html2='<tr><td>会议状态：</td><td> '+(data.status==1 ? '未开始' :'') +(data.status==3 ? '进行中' :'') + (data.status==2 ? '已结束' :'') +(data.status==0 ? '已取消' :'') +'</td></tr>';
           	$html2+='<tr><td>会议主题：</td><td>￥ '+data.subject+'</td></tr>';
           	$html2+='<tr><td>会议时间：</td><td>'+data.beginTime+'-'+data.endTime+'</td></tr>';
           	$html2+='<tr><td>会议时长：</td><td></td></tr>';
           	$html2+='<tr><td>预约人：</td><td>'+data.memberName+'</td></tr>';
           	$html2+='<tr><td>参会人：</td><td>'+$members+'</td></tr>';
           	$html2+='<tr><td>会议状态：</td><td>'+data.roomName+'</td></tr>';
           	$("#ViewInfo2").html($html2);   
           	
           	$html3='<tr><td>会议室区域：</td><td>￥ '+data.money+'</td></tr>';
        	$html3+='<tr><td>会议室空间：</td><td>'+data.spaceName+'</td></tr>';
        	$html3+='<tr><td>会议室名称：</td><td>'+data.roomName+'</td></tr>';
           	$("#ViewInfo3").html($html3);   
           	
           	$html4='<tr><td>订单总额：</td><td>￥ '+data.money+'</td></tr>';
        	$html4+='<tr><td>订单状态：</td><td>'+(data.payStatus==0 ? '已退订' :'') + (data.payStatus==1 ? '仅预订' :'')+ (data.payStatus==2 ? '已支付' :'')+'</td></tr>';
           	$("#ViewInfo4").html($html4);
   			
        },
        error: function () {
        	/*alert("error");*/
        }  
    });
}
function search(currentPage){
	var memberToken = localStorage.getItem('memberToken');
	var requestJson = getSearchParameter(currentPage);
	if(requestJson == false){
		return false;
	}
	$.ajax({
        url: '/euc/_onlineops/getMemberMeeting',
        data:requestJson,
       // data:'{"data":{"status":-1,"startTime":"2017-12-31","endTime":"2018-12-31","spaceId":0},"currentPage":1}',
        beforeSend: function(request) {
            request.setRequestHeader("token",memberToken );
        },
        
        dataType: 'JSON',
        async: true,
        contentType:'application/x-www-form-urlencoded',
        type: 'POST',
        success: function (result, textStatus, response) {
        	if(result.status === -2){
        		gotoLogin();
        	}
        	//alert(JSON.stringify(result));
        	pageBean = result.data;
        	$("#userTable").empty();	
        	
        	$.each(result.data.list,function(i,data){
        		       		

        		var bgcolor= localStorage.getItem('trcolor');
        		if(i % 2 == 0){
        			bgcolor = '';
        		}
        		var html = '<tr '+ bgcolor +'>';
        		html += '<td>' + $.trim(data.orderId) + '</td>';
        		if(data.payStatus==0){
        			html += '<td>已取消</td>';
        		}else{
            		html += '<td>' +(data.status==1 ? '未开始' :'') + (data.status==2 ? '已结束' :'') +(data.status==3 ? '进行中' :'') + '</td>';
        		}

        		html += '<td>' + data.memberName + '</td>';
        		html += '<td>' + data.subject + '</td>';
        		html += '<td>' + data.beginTime + '<br>'+data.endTime+'</td>';
        		html += '<td>' + data.spaceName + '</td>';
        		html += '<td>' + data.roomName + '</td>';
        		html += '<td>'+(data.payStatus==0 ? '已退订' :'') + (data.payStatus==1 ? '仅预订' :'')+ (data.payStatus==2 ? '已支付' :'')+'</td>';
        		html += '<td> ￥ ' + data.money + '</td>';
              		html += '<td><a href="javascript:detail('+data.orderId+');" class="tablelink">详情</a></td>';
        		html += '</tr>';
	        	$("#userTable").append(html);
	        	//dataList[data.id] = data;
			});
        	$("#totalRow").html(pageBean.totalRow+'&nbsp;');
        	$("#currentPage").html(pageBean.currentPage+'&nbsp;');
        	$("#pageSize").html(pageBean.pageSize+'&nbsp;');        	
        	var pageLength = 15;
        	initailPageNumber(pageBean.totalPage,pageBean.currentPage,pageLength);        	
        },
        error: function () {
        	console.log("error");
        }  
    });
}

function initailPageNumber(totalPage,currentPage,pageLength){
	try{
			var midNumber = Math.ceil((pageLength-2)/2);
			var paginList = $(".paginList");
			$(paginList).empty();			
			var prev = '<li class="paginItem"><a href="javascript:;"><span class="pagepre"></span></a></li>';
			var next = '<li class="paginItem"><a href="javascript:;"><span class="pagenxt"></span></a></li>';
			if( totalPage <= pageLength - 2){
				$(prev).appendTo($(".paginList"));
				for(var i = 1; i<totalPage+1; i++){
					var pagenItem = '<li class="paginItem"><a href="javascript:;">'+i+'</a></li>';
					if(i == currentPage){
						pagenItem = '<li class="paginItem current"><a href="javascript:;">'+i+'</a></li>';
					}			
					$(pagenItem).appendTo($(".paginList"));
				}
				$(next).appendTo($(".paginList"));
			}else{				
				if(currentPage <= midNumber){
					$(prev).appendTo($(".paginList"));
					for(var i=1;i <= pageLength - 4;i++){
						var pagenItem = '<li class="paginItem"><a href="javascript:;">'+i+'</a></li>';
						if(i == currentPage){
							pagenItem = '<li class="paginItem current"><a href="javascript:;">'+i+'</a></li>';
						}			
						$(pagenItem).appendTo($(".paginList"));
					}		
					var pagenItem = '<li class="paginItem more"><a href="javascript:;">...</a></li>';
					$(pagenItem).appendTo($(".paginList"));	
					var pagenItem = '<li class="paginItem"><a href="javascript:;">'+totalPage+'</a></li>';
					$(pagenItem).appendTo($(".paginList"));	
					$(next).appendTo($(".paginList"));
				}if(totalPage - currentPage < midNumber){

					var index = totalPage;
					for(var i=1;i <= pageLength - 4;i++){
						var pagenItem = '<li class="paginItem"><a href="javascript:;">'+index+'</a></li>';
						if(index == currentPage){
							pagenItem = '<li class="paginItem current"><a href="javascript:;">'+index+'</a></li>';
						}			
						$(pagenItem).prependTo($(".paginList"));
						index--;
					}		
					var pagenItem = '<li class="paginItem more"><a href="javascript:;">...</a></li>';
					$(pagenItem).prependTo($(".paginList"));	
					var pagenItem = '<li class="paginItem"><a href="javascript:;">'+ 1 +'</a></li>';
					$(pagenItem).prependTo($(".paginList"));
					$(prev).prependTo($(".paginList"));
					
					$(next).appendTo($(".paginList"));
				}else if(totalPage - currentPage >= midNumber && currentPage > midNumber){

					pagenItem = '<li class="paginItem current"><a href="javascript:;">'+currentPage+'</a></li>';
					$(pagenItem).prependTo($(".paginList"));
					var count = midNumber - 3;
					for(var i = 1; i<=count;i++){
						pagenItem = '<li class="paginItem"><a href="javascript:;">'+ (currentPage-i)+'</a></li>';
						$(pagenItem).prependTo($(".paginList"));
						pagenItem = '<li class="paginItem"><a href="javascript:;">'+ (currentPage+i)+'</a></li>';
						$(pagenItem).appendTo($(".paginList"));
					}
					pagenItem = '<li class="paginItem more"><a href="javascript:;">...</a></li>';
					$(pagenItem).prependTo($(".paginList"));	
					pagenItem = '<li class="paginItem"><a href="javascript:;">'+1	+'</a></li>';
					$(pagenItem).prependTo($(".paginList"));
					$(prev).prependTo($(".paginList"));
					
					pagenItem = '<li class="paginItem more"><a href="javascript:;">...</a></li>';
					$(pagenItem).appendTo($(".paginList"));	
					pagenItem = '<li class="paginItem"><a href="javascript:;">'+totalPage+'</a></li>';
					$(pagenItem).appendTo($(".paginList"));	
					$(next).appendTo($(".paginList"));
				}				
			}
			$(".paginItem").click(function(){
				if($(this).hasClass("more")){
					//alert("more");
					return;
				}else if($(this).find(".pagepre").length > 0){
					var cur = $('.paginItem.current').find("a").html();
					cur = parseInt(cur);
					if(cur > 1){
						search(cur - 1);
					}
				}else if($(this).find(".pagenxt").length > 0 ){
					var cur = $('.paginItem.current').find("a").html();
					cur = parseInt(cur);
					if(cur < totalPage){
						search(cur + 1);
					}
				}else{
					var cur = $(this).find("a").html();
					cur = parseInt(cur);
					search(cur);
				}
			});
	}catch(e){/*alert(e.messge);*/}	
}

/*空间名称*/
function initialSpace(){
	try{
		var spaceString = localStorage.getItem('space');
		spaces = $.parseJSON(spaceString);
	}catch(e){
		console.log(e.messge);
	}
	$.ajax({
        url: '/euc/system/space',
        data:'',
        beforeSend: function(request) {
        	var accessToken = localStorage.getItem('accessToken');
            request.setRequestHeader("accessToken",accessToken );
        },
        dataType: 'JSON',
        async: true,
        contentType:'application/json;charset=utf-8',
        type: 'POST',
        success: function (result, textStatus, response) {
        	try{
	        	$.each(result,function(index,data){	
	        		spaces[data.id] = data;  
	   			});
	        	var spaceJson = JSON.stringify(spaces);
	        	localStorage.setItem('space',spaceJson);
        	}catch(e){
        		/*alert(e.message);*/
        	}
        },
        error: function () {
        	/*alert("error");*/
        }  
    });
	var spaceSelect = $("#searchSpace");	

	$.each(spaces,function(key,space){	
		//if(space.id ==1){
			//$('<option value="'+ space.id +'" selected>'+ space.spaceName +'</option>').appendTo($(spaceSelect));
		//}else{
			$('<option value="'+ space.id +'">'+ space.spaceName +'</option>').appendTo($(spaceSelect));
		//}
	});
	
}